package com.micro.user.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.user.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户数据访问接口
 * 
 * @author micro
 * @since 1.0.0
 */
@Mapper
public interface UserRepository extends BaseMapper<User> {

    /**
     * 根据用户名查找用户
     */
    @Select("SELECT * FROM user_info WHERE username = #{username} AND deleted = 0")
    User findByUsername(@Param("username") String username);

    /**
     * 根据身份证号查找用户
     */
    @Select("SELECT * FROM user_info WHERE id_card_number = #{idCardNumber} AND deleted = 0")
    User findByIdCardNumber(@Param("idCardNumber") String idCardNumber);

    /**
     * 根据手机号查找用户
     */
    @Select("SELECT * FROM user_info WHERE phone_number = #{phoneNumber} AND deleted = 0")
    User findByPhoneNumber(@Param("phoneNumber") String phoneNumber);

    /**
     * 根据用户类型查找用户列表
     */
    @Select("SELECT * FROM user_info WHERE user_type = #{userType} AND deleted = 0")
    List<User> findByUserType(@Param("userType") Integer userType);

    /**
     * 查找所有活跃用户
     */
    @Select("SELECT * FROM user_info WHERE active = true AND deleted = 0")
    List<User> findAllActiveUsers();

    /**
     * 根据余额范围查找用户
     */
    @Select("SELECT * FROM user_info WHERE balance BETWEEN #{minBalance} AND #{maxBalance} AND deleted = 0")
    List<User> findByBalanceRange(@Param("minBalance") BigDecimal minBalance, @Param("maxBalance") BigDecimal maxBalance);

    /**
     * 统计用户数量
     */
    @Select("SELECT COUNT(*) FROM user_info WHERE deleted = 0")
    Long countUsers();

    /**
     * 统计指定用户类型的用户数量
     */
    @Select("SELECT COUNT(*) FROM user_info WHERE user_type = #{userType} AND deleted = 0")
    Long countByUserType(@Param("userType") Integer userType);
}
